<?php

include_once("../info_struct.php");

preg_match("/.+\/(.*)/", $_SERVER['SCRIPT_NAME'], $self_name);

/*
 * Escape Special Chars in POST
*/
function POST($str) {

    return htmlspecialchars($_POST[$str]);

}

if (isset($_POST['in_type'])) {

    include_once('wizard_input.php');
    $type = POST('in_type');

} else {

    $type = "";

}

$info_struct = new infos();

if ($type === "api") {

    $refs = api_input($info_struct);

} else if ($type === "manual") {

    $refs = manual_input($info_struct);

} else if ($type === "bibtex") {

    $refs = bibtex_input($info_struct);
    if ($refs === NULL) {
        $type = "";
        $error = "bibtex";
    }
}

if (isset($_POST['out'])) {

    include_once("wizard_summary.php");
    $finish_text = create_summary($info_struct);
    if ($finish_text === false)
        $finish = false;
    else
        $finish = true;

}
?>
<!DOCTYPE html>
<html class="no-js fuelux">
<head>

<title>References Wizard</title>

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

<link href="http://www.fuelcdn.com/fuelux/2.6.0/css/fuelux.min.css"
	rel="stylesheet" type="text/css">
<link href="CSS/main.css" rel="stylesheet" type="text/css">

<script src="http://code.jquery.com/jquery-latest.js"
	type="text/javascript"></script>
<script src="http://www.fuelcdn.com/fuelux/2.6.1/loader.min.js"
	type="text/javascript"></script>
<script src="JS/main.js" type="text/javascript"></script>

</head>

<body>

	<div id="wiz">
		<div id="MyWizard" class="wizard">
			<ul class="steps">
				<li data-target="#step1" class="active"><span
					class="badge badge-info">1</span>Select<span class="chevron"></span>
				</li>
				<li data-target="#step2"><span class="badge">2</span>Fill in<span
					class="chevron"></span>
				</li>
				<li data-target="#step3"><span class="badge">3</span>Validate<span
					class="chevron"></span>
				</li>
				<li data-target="#step4"><span class="badge">4</span>Summary<span
					class="chevron"></span>
				</li>
			</ul>
		</div>
		<?php

		/*
		 *
		*      Select step
		*
		*
		*/

		?>
		<div class="step-content">
			<div class="step-pane active" id="step1">
				<span class="description"> Select one of the following methods to
					add a new reference to the system and to your article.<br> <br>
					Choose:<br> <br> - 'Manual Input' to fill in the reference details
					yourself, e.g. Title, Author, Year, etc.<br> If you need to write
					your own proof and you don't have a title or an author, click the
					"Note" button on the bottom.<br> <br> - 'Bibtex Input' to paste the
					BibTeX reference you are referring to.<br> <br> - 'ID Input' to add
					the referenced article by a DOI, ZMATH or arXiv ID.<br>
				</span> <br>
				<div id="select">
					<table>
						<tr>
							<td><input class="btn" type="button" id="manual_btn"
								onclick="change('manual');" value="Manual Input"></td>
						</tr>
						<tr>
							<td><input class="btn" type="button" id="bibtex_btn"
								onclick="change('bibtex');" value="Bibtex Input"></td>
						</tr>
						<tr>
							<td><input class="btn" type="button" id="api_btn"
								onclick="change('api');" value="ID Input"></td>
						</tr>
					</table>
				</div>
			</div>
			<?php

			/*
			 *
			*      Input step
			*
			*
			*/

			?>
			<?php

			/*
			 *
			*      Input Step - ID Input
			*
			*
			*/

			?>
			<div class="step-pane" id="step2">
				<div id="api_form">
					<span class="description">Add the article you are referring to by a
						DOI, ZMATH or arXiv ID.<br>
					</span>
					<div class="form">
						<form id="form_fix"
							action="<?php echo (isset($self_name[1]) ? $self_name[1] : "fuelux_wizard.php" )?>"
							method="post">
							<div class="input-append input-add">
								<input type="text" name="api" style="height: 25px;"
									value="<?php echo isset($_POST['api']) ? POST('api') : '' ?>" />
							</div>
							<div id="myCombobox" class="select btn-group" data-resize="auto">
								<button type="button" data-toggle="dropdown"
									class="btn dropdown-toggle">
									<span class="dropdown-label" id="selected_api"></span><span
										class="caret"></span>
								</button>
								<ul class="dropdown-menu">
									<li data-value="1"><a href="#">DOI</a></li>
									<li data-value="2"><a href="#">ArXiv</a></li>
									<li data-value="3"><a href="#">ZMath</a></li>
								</ul>
							</div>
							<input type="hidden" name="in_type" value="api"> <input
								type="hidden" name="select_api"> <br> <input class="btn"
								type="button" onclick="change('');" value="Back"> <input
								class="btn" type="submit" onclick="return IsEmpty('api');"
								value="Next" />
						</form>
					</div>
				</div>
				<?php

				/*
				 *
				*      Input Step - Manual Input
				*
				*
				*/

				?>
				<div id="manual_form">
					<span class="description">Add the article you are referring to by
						filling in the details below.<br> If you need to write your own
						proof and you don't have a title or an author, click the 'Note'
						button on the bottom.<br>
					</span> <br>
					<div class="form">
						<form id="form_fix"
							action="<?php echo (isset($self_name[1]) ? $self_name[1] : "fuelux_wizard.php" )?>"
							method="post">
							<table>
								<tr>
									<td>Title*:</td>
									<td class="man_input"><input type="text" name="title"
										style="margin: 0px;"
										value="<?php echo isset($_POST['title']) ? POST('title') : '' ?>">
									</td>
									<td class="example">e.g.: On Computable Numbers, with an
										Application to the Entscheidungsproblem</td>
								</tr>
								<tr>
									<td>Year*:</td>
									<td class="man_input"><input type="text" name="year"
										style="margin: 0px;"
										value="<?php echo isset($_POST['year']) ? POST('year') : '' ?>">
										<label class="checkbox checkbox-custom"><input type="checkbox"
											name="bc" value="yes"><i class="checkbox"></i>before Christ</label>
									</td>
									<td class="example">e.g.: 1937</td>
								</tr>
								<tr>
									<td>Author*:</td>
									<td class="man_input"><input type="text" name="author"
										style="margin: 0px;"
										value="<?php echo isset($_POST['author']) ? POST('author') : '' ?>">
									</td>
									<td class="example">e.g.: Alan Turing</td>
								</tr>
								<tr>
									<td>DOI:</td>
									<td class="man_input"><input type="text" name="doi"
										style="margin: 0px;"
										value="<?php echo isset($_POST['doi']) ? POST('doi') : '' ?>">
									</td>
									<td class="example">e.g.: 10.1112/plms/s2-42.1.230</td>
								</tr>
								<tr>
									<td>ZMath:</td>
									<td class="man_input"><input type="text" name="zmath"
										style="margin: 0px;"
										value="<?php echo isset($_POST['zmath']) ? POST('zmath') : '' ?>">
									</td>
									<td class="example">e.g.:</td>
								</tr>
								<tr>
									<td>Arxiv:</td>
									<td class="man_input"><input type="text" name="arxiv"
										style="margin: 0px;"
										value="<?php echo isset($_POST['arxiv']) ? POST('arxiv') : '' ?>">
									</td>
									<td class="example">e.g.:</td>
								</tr>
								<tr>
									<td>Journal:</td>
									<td class="man_input"><input type="text" name="journal"
										style="margin: 0px;"
										value="<?php echo isset($_POST['journal']) ? POST('journal') : '' ?>">
									</td>
									<td class="example">e.g.: Proceedings of the London
										Mathematical Society</td>
								</tr>
								<tr>
									<td>Volume:</td>
									<td class="man_input"><input type="text" name="volume"
										style="margin: 0px;"
										value="<?php echo isset($_POST['volume']) ? POST('volume') : '' ?>">
									</td>
									<td class="example">e.g.: s2-42</td>
								</tr>
								<tr>
									<td>Number:</td>
									<td class="man_input"><input type="text" name="number"
										style="margin: 0px;"
										value="<?php echo isset($_POST['number']) ? POST('number') : '' ?>">
									</td>
									<td class="example">e.g.:</td>
								</tr>
								<tr>
									<td>Pages:</td>
									<td class="man_input"><input type="text" name="pages"
										style="margin: 0px;"
										value="<?php echo isset($_POST['pages']) ? POST('pages') : '' ?>">
									</td>
									<td class="example">e.g.: 230-265</td>
								</tr>
								<tr>
									<td>Month:</td>
									<td class="man_input"><input type="text" name="month"
										style="margin: 0px;"
										value="<?php echo isset($_POST['month']) ? POST('month') : '' ?>">
									</td>
									<td class="example">e.g.: January</td>
								</tr>
								<tr>
									<td>Other:</td>
									<td class="man_input"><input type="text" name="other"
										style="margin: 0px;"
										value="<?php echo isset($_POST['other']) ? POST('other') : '' ?>">
									</td>
									<td class="example">e.g.:</td>
								</tr>
							</table>
							<label>* Indicates required field.</label> <input type="hidden"
								name="in_type" value="manual"> <br> <a id="note_btn" class="btn"
								href="javascript:void(0);"
								onclick='document.getElementById("note").style.display = "block"; document.getElementById("note_btn").style.display = "none";'>Note</a>
							<br>
							<textarea id="note" name="note" cols="50" rows="10">
								<?php echo isset($_POST['note']) ? POST('note') : '' ?>
							</textarea>
							<br> <input class="btn" type="button" onclick="change('');"
								value="Back"> <input class="btn" type="submit"
								onclick="return IsEmpty('manual');" value="Next" />
						</form>
					</div>
				</div>
				<?php

				/*
				 *
				*      Input Step - Bibtex Input
				*
				*
				*/

				?>
				<div id="bibtex_form">
					<span class="description">Add the article you are referring to by
						pasting the BibTeX reference.<br>
					</span>
					<div class="form">
						<form id="form_fix"
							action="<?php echo (isset($self_name[1]) ? $self_name[1] : "fuelux_wizard.php" )?>"
							method="post">
							<textarea id="bibtex_in" name="bib_in" cols="50" rows="10">
								<?php echo isset($_POST['bib_in']) ? POST('bib_in') : '' ?>
							</textarea>
							<input type="hidden" name="in_type" value="bibtex"> <br> <input
								class="btn" type="button" onclick="change('');" value="Back"> <input
								class="btn" type="submit" onclick="return IsEmpty('bib');"
								value="Next" />
						</form>
					</div>
				</div>

			</div>
			<?php

			/*
			 *
			*      Validate step
			*
			*
			*/

			?>
			<div class="step-pane" id="step3">
				<span class="description">Please select the proper references. <br>
					If you would like to add additional information, click the 'Note'
					button below.
				</span>
				<?php
				if ($type !== "") {
?>
				<script type="text/javascript">
			/* Fix to be on the valdiate step */
			$('#MyWizard').wizard('selectedItem', { step: 3 });
			change('<?=$type ?>', 0);
		</script>
				<?php
				if (!isset($error)) {
?>
				<form id="validate"
					action="<?php echo (isset($self_name[1]) ? $self_name[1] : "fuelux_wizard.php" )?>"
					method="post">
					<?php
					include_once("wizard_validate.php");
					if ($info_struct->title !== "" && $info_struct->author!== "" && $info_struct->year !== "" && $info_struct->title !== NULL && $info_struct->author!== NULL && $info_struct->year !== NULL) {
			echo "<h2>".from_to_db_name("man")."</h2><br>";
			createSelect($info_struct, 0);
			if ($info_struct->note !== "")
			    echo $info_struct->note."<br>";
		} elseif ($info_struct->note !== "" && $info_struct->note !== NULL) {
			if ($info_struct->title !== "" && $info_struct->title !== NULL)
			    createSelect($info_struct, 0);
			else {
?>
					<input type="hidden" name="noteman0"
						value='<?=base64_encode(serialize($info_struct));?>'>
					<?php
			}
			echo $info_struct->note."<br>";
		}
		if (count($refs) > 0 && $refs !== false) {
			$old_ref = "";
			$ref_count = 0;
			for ($i = 0; $i < count($refs); $i++) {
				if ($old_ref != $refs[$i]->from) {
					if ($old_ref != "") {
						$blank = new infos();
						$blank->from = $old_ref."_blank";
						$ref_count++;
						createSelect($blank, $ref_count."_blank");
					}

					$old_ref = $refs[$i]->from;
					$ref_count = 0;
					echo "<h2>".from_to_db_name($old_ref)."</h2><br>";
				} else {
					$ref_count++;
				}
				createSelect($refs[$i], $ref_count);
			}
			$blank = new infos();
			$blank->from = $old_ref."_blank";
			$ref_count++;
			createSelect($blank, $ref_count."_blank");
		}
		?>
					<label id="selection_error"></label> <input type="hidden"
						name="out" value="out"> <a id="add_note_btn" class="btn"
						href="javascript:void(0);"
						onclick='document.getElementById("extra_note_visible").value = "visible"; document.getElementById("add_note").style.display = "block"; document.getElementById("add_note_btn").style.display = "none";'>Note</a>
					<br>
					<textarea id="add_note" name="extra_note" cols="50" rows="10">
						<?php echo ((isset($_POST['note']) && $_POST['note'] !== "") ? POST('note') : ((isset($refs[0]) && $refs[0]->from === "ldb" && $refs[0]->note !== "") ? $refs[0]->note : '')) ?>
					</textarea>
					<input type="hidden" id="extra_note_visible"
						name="extra_note_visible" value="hidden"> <input class="btn"
						type="button" onclick="change('<?=$type?>', 2);" value="Back"> <input
						class="btn" type="submit" onclick="return checkValid();"
						value="Next" />
				</form>
				<?php
	}
} else if ($type === "" && isset($error) && $error === "bibtex") {
?>
				<script type="text/javascript">
			/* Fix to go to the bibtex input because there was an error */
			change('<?=$error ?>');
			document.getElementsByName("bib_in")[0].style.border = "1px solid red";
			document.getElementById("bibtex_form").innerHTML += "<br><p style=\"padding: 20px; font-size: 20px; color: red;\">Error Bibtex Code is missing either title, author or year!<p/>";
		</script>
				<?php
}
?>
			</div>
			<?php

			/*
			 *
			*      Summary and Last Step
			*
			*
			*/

			?>
			<div class="step-pane" id="step4">
				<?php
				if (isset($finish) && $finish === true) {
?>
				<script type="text/javascript">
			/* Fix to be on the last step */
			$('#MyWizard').wizard('selectedItem', { step: 4 });
		</script>
				<?php
				include_once("../helper_funcs.php");
				info_struct_to_html($info_struct);
				echo "<br>";
				echo $finish_text;
				echo "<br>";
				?>
				<form>
					<input class="btn" type="button" onclick="close_popup();"
						value="Close" />
				</form>
				<?php
} elseif (isset($finish) && $finish === false) {
?>
				<script type="text/javascript">
			/* Fix to be on the last step */
			$('#MyWizard').wizard('selectedItem', { step: 4 });
		</script>

				Something went wrong: you didn't select anything.<br> Please return
				to step 1 and try again.<br>
				<?php
} else {
	echo "Missing Data";
}
?>
			</div>
		</div>
	</div>
	<script>
$(document).ready(function() {
	var wizard = $('#MyWizard');

	/* Fix select method if we go back */
	wizard.on('changed', function(e, data) {
		if (data.currentStep === 1) {
			change('');
		}
	});

<?php
if (isset($_POST['select_api'])) {
?>
	/* Fix Combobox if we go back */
	$('#myCombobox').select('selectByText', '<?=POST('select_api')?>');
<?php
}

if (isset($finish) && $finish === true) {
?>
	/* Fix selection if there was an error to be able to go back */
	wizard.on('stepclick', function(e, data) {
		console.log('step' + data.step + ' clicked from ' + wizard.wizard('selectedItem').step);
		if(wizard.wizard('selectedItem').step === 4) {
			return e.preventDefault();
		}
	});
<?php
}  elseif (isset($finish) && $finish === false) {
?>
	/* Fix selection so you can't go back */
	wizard.on('stepclick', function(e, data) {
		console.log('step' + data.step + ' clicked from ' + wizard.wizard('selectedItem').step);
		if(wizard.wizard('selectedItem').step === 4 && (data.step === 3 || data.step === 2)) {
			return e.preventDefault();
		}
	});
<?php
}
?>
});
</script>
</body>
</html>
